
/**
 * @Description:
 * @author 若琳
 * @date 2021/8/12
*/

const schedule = require("node-schedule");
const {MYSQL} = require("../../../../../../ruoling-common/src/com/ruoling/common/config/mysqlconfig.js");
const Pager_DB = require("../table/Pager_DB.js");



const scheduleCronstyle = async ()=>{

    var rule = new schedule.RecurrenceRule();

    //每5分钟执行一次
    rule.minute = [0,5,10,15,20,25,30,35,40,45,50,55];

    schedule.scheduleJob(rule, async function(){
        console.log('schedule--online......!'+new Date());
        await OnlineOf();
        await OnlineTimeout();

    });

}
scheduleCronstyle();

//判断是否强退与超时
const  OnlineOf = async ()=>{
    let startTrans = null;

    try {
        let connect = await MYSQL().connect();
        startTrans = await connect.startTrans();

        let obj = {
            sql:"DELETE FROM rl_sys_user_online WHERE sys_user_login_state = \"off_line\""
        };

        await startTrans.query(obj.sql);

        await startTrans.commit();

        return 1;
    }catch (e){
        console.log(e)
        await startTrans.rollback();
        return e;
    }
};

const OnlineTimeout = async ()=>{

    let startTrans = null;
    try {
        let connect = await MYSQL().connect();
        startTrans = await connect.startTrans();

        let obj = {
            sql:"SELECT * FROM rl_sys_user_online WHERE Is_delete = 1"
        };

        let data = await startTrans.query(obj.sql);

        if(data.length == 0){
            await startTrans.commit();
            return 1;
        }

        for(let i = 0;i<data.length;i++){

             let start_time = new Date(data[i].sys_start_time_stamp).getTime()/1000;

             let end_time = new Date().getTime()/1000;

             let total_time = end_time-start_time;

            if(total_time >= data[i].sys_expire_time){
                //超时状态
                await startTrans.query("DELETE FROM rl_sys_user_online WHERE sys_user_sessionid = \""+data[i].sys_user_sessionid+"\"");
            }
        }

        await startTrans.commit();
        return 1;
    }catch (e){
        await startTrans.rollback();
        return e;
    }
}

module.exports = 1;


// 30 * * * * *  每分钟的第三十秒
// 30 1 * * * * 每小时的第一分钟第三十秒
// 30 1 1 * * * 每天凌晨1点第一分钟第三十秒
// 30 1 1 1 * * 每月第一天凌晨一点第一分钟第三十秒    * * * 21 * *
